package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.StaticInfoQry;
import com.jftone.bh.app.model.*;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.*;

import java.util.List;
import java.util.Map;

@Service
public class RecordService {

	
	@Autowired
	private Dao dao;

	public List<IData<String, Object>> queryList(IData<String, Object> data, Page page) throws DaoException {
		String cfNo = data.getString("cfNo", "");
		String status = data.getString("status", "");
		String userId = data.getString("userId", "");
		String sTime = data.getString("sTime", "");
		String eTime = data.getString("eTime", "");
		String currency = data.getString("currency", "");
		String type = data.getString("type", "");
		String mtype = data.getString("mtype", "");
		IData<String, Object> param = new DataMap<String, Object>();
		if(!StringUtil.isBlank(userId)){
			param.put("USER_ID", userId);
		}
		if(!StringUtil.isBlank(cfNo)){
			param.put("CF_NO", "%"+cfNo +"%");
		}
		if(!StringUtil.isBlank(status)){
			param.put("STATUS", status);
		}
		if(!StringUtil.isBlank(currency)){
			param.put("CURRENCY", currency);
		}
		if(!StringUtil.isBlank(type)){
			param.put("TYPE", type);
		}
		if(!StringUtil.isBlank(mtype)){
			param.put("MTYPE", mtype);
		}
		if(!StringUtil.isBlank(sTime)){
			param.put("S_TIME", sTime+" 00:00:00");
		}
		if(!StringUtil.isBlank(eTime)){
			param.put("E_TIME", eTime+" 00:00:00");
		}
		List<IData<String, Object>> list = dao.query("queryRecordList", param, page);
		NtbUser ntbUser;
		SysAdmin sysAdmin;
		short recordType;
		for (IData<String, Object> record:list) {
			param.clear();
			param.put("id",record.getInt("ACCEPTER"));
			ntbUser = dao.get(NtbUser.class,param);
			if(ntbUser != null){
				record.put("ACCOUNT", ntbUser.getAccount());
				record.put("USER_NAME", ntbUser.getUserName());
			}
			recordType = record.getShort("TYPE",SysConst.RECORD_TYPE_0);
			if(SysConst.RECORD_TYPE_2 != recordType && SysConst.RECORD_TYPE_3 != recordType){
				record.put("OPERATION", record.getString("ACCOUNT"));
			}
		}

		return list;
	}

	public List<IData<String, Object>> queryWithdrawList(IData<String, Object> data, Page page) throws DaoException {
		String cfNo = data.getString("cfNo", "");
		String status = data.getString("status", "");
		String userId = data.getString("userId", "");
		String sTime = data.getString("sTime", "");
		String eTime = data.getString("eTime", "");
		IData<String, Object> param = new DataMap<String, Object>();
		if(!StringUtil.isBlank(userId)){
			param.put("USER_ID", userId);
		}
		if(!StringUtil.isBlank(cfNo)){
			param.put("CF_NO", "%"+cfNo +"%");
		}
		if(!StringUtil.isBlank(status)){
			param.put("STATUS", status);
		}
		param.put("TYPE", SysConst.RECORD_TYPE_4);
		if(!StringUtil.isBlank(sTime)){
			param.put("S_TIME", sTime+" 00:00:00");
		}
		if(!StringUtil.isBlank(eTime)){
			param.put("E_TIME", eTime+" 00:00:00");
		}
		List<IData<String, Object>> list = dao.query("queryRecordList", param, page);
		NtbUser ntbUser;
		List<IData<String, Object>> stateList;
		Map<String, String> stateMap = StaticInfoQry.getParamMap(SysConst.RECORD_WITHDRAW_STATUS);
		for (IData<String, Object> record:list) {
            param.clear();
			param.put("id",record.getInt("ACCEPTER"));
			ntbUser = dao.get(NtbUser.class,param);
			if(ntbUser != null){
				record.put("ACCOUNT", ntbUser.getAccount());
				record.put("USER_NAME", ntbUser.getUserName());
			}
			param.clear();
			param.put("RECORD_ID",record.getLong("ID"));
			stateList = dao.query("queryWithdrawStateList",param);
			if(stateList != null && !stateList.isEmpty()){
				IData<String, Object>  stateObj = stateList.get(0);
				record.put("AUDIT_STATUS_NAME", stateMap.get(stateObj.getString("STATUS")));
				record.put("AUDIT_UPDATE_TIME", stateObj.getLong("UPDATE_TIME"));
				record.put("AUDIT_UPDATE_USER",stateObj.getString("OPERATION"));
			}

		}

		return list;
	}

	public IData<String, Object> getWithdrawData(long recordId) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("ID",recordId);
		param.put("TYPE",SysConst.RECORD_TYPE_4);
		IData<String, Object> record = dao.get("getRecordById",param);

		param.clear();
		param.put("id",record.getInt("ACCEPTER"));
		NtbUser ntbUser = dao.get(NtbUser.class,param);
		record.put("ACCOUNT", ntbUser.getAccount());
		record.put("USER_NAME", ntbUser.getUserName());

		param.clear();
		param.put("RECORD_ID",recordId);
		List<IData<String, Object>> stateList = dao.query("queryWithdrawStateList",param);
		Map<String, String> stateMap = StaticInfoQry.getParamMap(SysConst.RECORD_WITHDRAW_STATUS);
		if(stateList != null && !stateList.isEmpty()){
			IData<String, Object>  stateObj = stateList.get(0);
			record.put("AUDIT_STATUS", stateObj.getString("STATUS"));
			record.put("AUDIT_UPDATE_TIME", stateObj.getLong("UPDATE_TIME"));
			record.put("AUDIT_UPDATE_USER",stateObj.getString("OPERATION"));
			for (IData<String, Object> state:stateList) {
				state.put("AUDIT_STATUS_NAME",stateMap.get(state.getString("STATUS")));
			}
		}else{
			record.put("AUDIT_STATUS", record.getString("STATUS"));
			record.put("AUDIT_UPDATE_TIME", 0);
			record.put("AUDIT_UPDATE_USER", "");
		}
		record.put("AUDIT_STATUS_NAME",stateMap.get(record.getString("AUDIT_STATUS")));
		record.put("stateList",stateList);

		return record;
	}

	public synchronized IData<String, Object> saveWithdrawAudit(long recordId, short auditStatus,String remark,String operation) throws DaoException {
		IData<String, Object> retData = new DataMap<String, Object>();
		retData.put("resultCode",0);
		retData.put("resultMsg","");
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id",recordId);
		param.put("type",SysConst.RECORD_TYPE_4);
		NtbRecord record = dao.get(NtbRecord.class,param);
		short status = record.getStatus();
		if(record == null){
			retData.put("resultCode",1);
			retData.put("resultMsg","提现数据不存在");
		}else if(status>SysConst.RECORD_WITHDRAW_STATUS_4){
			retData.put("resultCode",1);
			retData.put("resultMsg","提现状态已锁定");
		}else if(auditStatus<status){
			retData.put("resultCode",1);
			retData.put("resultMsg","审核状态不能回退");
		}

		if(auditStatus==SysConst.RECORD_WITHDRAW_STATUS_3 ||auditStatus==SysConst.RECORD_WITHDRAW_STATUS_4){
			NtbRecord recordNew = new NtbRecord();
			recordNew.setId(recordId);
			recordNew.setStatus(auditStatus==SysConst.RECORD_WITHDRAW_STATUS_3?SysConst.RECORD_STATUS_1:SysConst.RECORD_STATUS_2);
			dao.update(recordNew);
			if(auditStatus==SysConst.RECORD_WITHDRAW_STATUS_4){
				param.clear();
				param.put("id",record.getAccepter());
				NtbUser user = dao.get(NtbUser.class,param);
				NtbUser userTemp = new NtbUser();
				userTemp.setId(user.getId());
				userTemp.setStampMoney(user.getStampMoney()+Math.abs(record.getAmount()));
				dao.update(userTemp);
			}
		}
		NtbWithdrawState ntbWithdrawState = new NtbWithdrawState();
		ntbWithdrawState.setRecordId(recordId);
		ntbWithdrawState.setRemark(remark);
		ntbWithdrawState.setStatus(auditStatus);
		ntbWithdrawState.setUpdateTime(DateUtil.getMillisecond());
		ntbWithdrawState.setOperation(operation);
		dao.save(ntbWithdrawState);

		return retData;
	}
}
